package com.java.framework.web.service;

import com.java.common.utils.tool.StringUtils;
import com.java.common.utils.tool.Tool;
import com.java.framework.cache.CacheUtil;
import com.java.project.system.city.domain.City;
import com.java.project.system.city.service.ICityService;
import com.java.project.system.dist.domain.Dist;
import com.java.project.system.dist.service.IDistService;
import com.java.project.system.prov.domain.Prov;
import com.java.project.system.prov.service.IProvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 首创 html调用 thymeleaf 获取省、市、区
 *
 * @author admin_wjz
 */
@Service("area")
public class AreaService {

    /**
     * 所有的省份集合
     */
    private static final String PROV_LIST = "prov_list";
    /**
     * 所有的城市集合
     */
    private static final String CITY_LIST = "city_list";
    /**
     * 所有的区域集合
     */
    private static final String DIST_LIST = "dist_list";
    /**
     * 某省份的所有城市集合
     */
    private static final String CITY_MAP = "city_map";
    /**
     * 某城市的所有区域集合
     */
    private static final String DIST_MAP = "dist_map";

    @Autowired
    private IProvService provService;
    @Autowired
    private ICityService cityService;
    @Autowired
    private IDistService distService;

    /**
     * 获取所有省信息
     *
     * @return 省
     */
    public List<Prov> getProvList() {
        // 从redis中获取
        List<Prov> list = (List<Prov>) CacheUtil.get(PROV_LIST);
        // 如果redis中不存在
        if (StringUtils.isEmpty(list)) {
            // 查询数据库
            list = provService.selectProvList(null);
            // 存入redis中
            CacheUtil.set(PROV_LIST, list);
        }
        return list;
    }

    /**
     * 获取所有市信息
     *
     * @return 市
     */
    public List<City> getCityList() {
        List<City> list = (List<City>) CacheUtil.get(CITY_LIST);
        if (StringUtils.isEmpty(list)) {
            list = cityService.selectCityList(null);
            CacheUtil.set(CITY_LIST, list);
        }
        return list;
    }

    /**
     * 获取所有区信息
     *
     * @return 区
     */
    public List<Dist> getDistList() {
        List<Dist> list = (List<Dist>) CacheUtil.get(DIST_LIST);
        if (StringUtils.isEmpty(list)) {
            list = distService.selectDistList(null);
            CacheUtil.set(DIST_LIST, list);
        }
        return list;
    }

    /**
     * 根据省ID获取市信息
     *
     * @param provId
     * @return 市
     */
    public List<City> getCityList(String provId) {
        List<City> list = (List<City>) CacheUtil.hget(CITY_MAP, provId);
        if (StringUtils.isEmpty(list)) {
            list = cityService.selectCityList(new City(provId));
            CacheUtil.hset(CITY_MAP, provId, list);
        }
        return list;
    }

    /**
     * 根据市ID获取区信息
     *
     * @param cityId
     * @return 区
     */
    public List<Dist> getDistList(String cityId) {
        List<Dist> list = (List<Dist>) CacheUtil.hget(DIST_MAP, cityId);
        if (StringUtils.isEmpty(list)) {
            list = distService.selectDistList(new Dist(cityId));
            CacheUtil.hset(DIST_MAP, cityId, list);
        }
        return list;
    }

}
